package com.casic.license.view;


import com.ddxz.tool.core.convert.DateUtil;
import com.ddxz.tool.core.security.SecurityUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/7/22
 */

public class GeneratorListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("generator.......");
        StringBuilder builder = new StringBuilder();
        Map<String, Component> components = ((LicenseWindow)jFrame).getAllComponent();
        builder.append(((JTextField)components.get("companyName")).getText());
        builder.append(".");
        if(((JCheckBox)components.get("server")).isSelected()) {
            builder.append("APICUSTOM").append(",").append("APIMASTER").append(",").append("APISYNC").append(",");
        }

        if(((JCheckBox)components.get("asset")).isSelected()) {
            builder.append("WORKFLOW").append(",").append("ASSETMANAGER").append(",").append("ASSETOPEN").append(",");
        }

        if(((JCheckBox)components.get("blood")).isSelected()) {
            builder.append("BLOODNEO").append(",");
        }

        if(((JCheckBox)components.get("meta")).isSelected()) {
            builder.append("METADATACOLLECT").append(",").append("METADATAOPEN").append(",").append("METADATAMANAGER").append(",");
        }

        if(((JCheckBox)components.get("user")).isSelected()) {
            builder.append("PERSONALSOURCES").append(",").append("PERSONALSUBSCRIPTION").append(",");
        }

        if(((JCheckBox)components.get("quality")).isSelected()) {
            builder.append("QUALITYENTITY").append(",")
                    .append("QUALITYPDF").append(",")
                    .append("QUALITYREPORT").append(",")
                    .append("QUALITYRULE").append(",")
                    .append("QUALITYSCHEDULE").append(",")
                    .append("QUALITYTABLE").append(",")
                    .append("QUALITYTASK").append(",")
                    .append("REPORTSERVER").append(",");
        }

        if(((JCheckBox)components.get("schedule")).isSelected()) {
            builder.append("SCHEDULE").append(",");
        }

        if(((JCheckBox)components.get("standard")).isSelected()) {
            builder.append("STANDARD").append(",");
        }

        if(((JCheckBox)components.get("tag")).isSelected()) {
            builder.append("TAG").append(",");
        }

        if(((JCheckBox)components.get("common")).isSelected()) {
            builder.append("TASKLOG").append(",")
                    .append("TENANT").append(",")
                    .append("SYSTEM").append(",")
                    .append("AUTH").append(",")
                    .append("ZUUL").append(",")
                    .append("COMMONLOGGING").append(",");
        }

        builder.deleteCharAt(builder.length() - 1);

        builder.append(".").append(getExpired(((JComboBox)components.get("unit")).getSelectedIndex(), (JTextField)components.get("validDate")));

        ((JTextArea)components.get("license")).setText(SecurityUtil.rsaEncode(builder.toString()));
    }

    private long getExpired(int idx, JTextField strDt) {
        long expired = DateUtil.getTimestamp();
        long dt = Long.parseLong(strDt.getText());

        switch (idx) {
            case 0:
                expired += dt * DateUtil.MILLISECOND_PER_DAY;
                break;
            case 1:
                expired += 7 * dt * DateUtil.MILLISECOND_PER_DAY;
                break;
            case 2:
                expired += 30 * dt * DateUtil.MILLISECOND_PER_DAY;
                break;
            case 3:
                expired += 365 * dt * DateUtil.MILLISECOND_PER_DAY;
                break;
            default:
                return expired;
        }

        return expired;
    }

    public GeneratorListener(JFrame jFrame) {
        this.jFrame = jFrame;
    }

    private JFrame jFrame;
}
